<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserRequest;
use App\Repositories\UserRepository;
use Illuminate\View\View;

class UserController extends Controller
{
    private $_userRepository;

    public function __construct()
    {
        $this->_userRepository = new UserRepository();
    }

    /**
     * 用户列表视图
     * @return View
     */
    public function index()
    {
        $paginator = $this->_userRepository->getPageList();
        $search = request('search', '');
        return view('user.index')->with(compact(["paginator", "search"]));
    }

    /**
     * 新增用户视图
     * @return View
     */
    public function create()
    {
        return view('user.create');
    }

    /**
     * 添加用户
     * @param UserRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(UserRequest $request)
    {
        $this->_userRepository->store($request);
        return redirect('user');
    }

    /**
     * 编辑用户视图
     * @param $id
     * @return View
     */
    public function edit($id)
    {
        $user = $this->_userRepository->edit($id);
        return view('user.edit')->with(compact("user"));
    }

    /**
     * 更新用户
     * @param UserRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UserRequest $request,$id)
    {
        $this->_userRepository->update($request,$id);
        return redirect('user');
    }

    /**
     * 删除用户
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        $this->_userRepository->destroy($id);
        return redirect('user');
    }
}
